package com.hutu.core.config;

import com.hutu.core.aspect.SysAspect;
import com.hutu.util.RobotUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;

@Configuration
@Slf4j
@Component
public class AsyncExceptionConfig implements AsyncConfigurer {

    @Autowired
    private RobotUtil robotUtil;

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SpringAsyncExceptionHandler();
    }

    class SpringAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
        @Override
        public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
            log.error("------进入Async无返回方法的异常处理方法---------");
            String err = "请求参数：" + Arrays.stream(objects).collect(Collectors.toList());
            err += "\r\n【监控到系统异常】" + SysAspect.getErrorMsg(throwable);
            robotUtil.sendMsg(err);
            log.error(err);
        }
    }
}
